package com.example.gomi.intelligentcitypipe.serial;

import android.util.Log;

import com.example.gomi.intelligentcitypipe.TimerReaderThread;

/**
 * Created by GoMi on 16/7/9.
 */
public class PipeProtocol {


    public static final byte[] RECEIVE_HEAD = new byte[]{0x01, 0x0B, 0x00, 0x7E};


    private int mSendPackageCount = 0;
    private int mReceivePackageCount = 0;

private String version = null;
    public final static byte COMMAND_BATTERY_LEVEL = 0x00;
    public final static byte COMMAND_READ_PAGE3 = 0x03;
    public final static byte COMMAND_READ_PAGE9 = 0x09;
    public final static byte COMMAND_WRITE_PAGE9 = 0x19;
    public final static byte COMMAND_READ_PAG11 = 0x0a;
    public final static byte COMMAND_WRITE_PAG11 = 0x0b;
    public final static byte COMMAND_HUMITURE_READ = 0x0c;
    private final static int COMMAND_INDEX = 3;

    private final static byte[] QUERY_COMMAND = new byte[]{
            (byte) 0xff,
            (byte) 0xff,
            (byte) 0xff,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            (byte) 0xff
    };

    private final static byte[] WRITE_COMMAND = new byte[]{
            (byte) 0xff,
            (byte) 0xff,
            (byte) 0xff,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            (byte) 0xff
    };
    private final static byte[] HUMITURE_COMMAND = new byte[]{
            (byte) 0xff,
            (byte) 0xff,
            (byte) 0xff,
            0x0c,
            0x00,
            0x01,
            0x00,
            (byte) 0xFF,
            0x00,
            0x00

    };
    public final static byte SERIAL_NO_DATA = 0x01;

    public final static byte BATTERY_FULL = 0x03;
    public final static byte BATTERY_MEDIUM = 0x01;
    public final static byte BATTERY_LOW = 0x00;


    TimerReaderThread mTimerThread = null;

    private Byte mCurrentCommand = COMMAND_READ_PAGE3;


    public PipeProtocol(String version) {
        this.version = version;
    }

    public boolean startGetPipeInfo() {
        if (mTimerThread == null) {
            mTimerThread = new TimerReaderThread(this,this.version);
        }
        mTimerThread.start();
        return true;
    }

    public void getBaterryLevel() {
        mCurrentCommand = COMMAND_BATTERY_LEVEL;
        QUERY_COMMAND[COMMAND_BATTERY_LEVEL] = COMMAND_BATTERY_LEVEL;
        SerialController.getInstance().sendData(QUERY_COMMAND);
    }

    public void getPipeId() {
        mCurrentCommand = COMMAND_READ_PAGE3;
        QUERY_COMMAND[COMMAND_INDEX] = COMMAND_READ_PAGE3;
        SerialController.getInstance().sendData(QUERY_COMMAND);
    }

    public void getPage9() {
        mCurrentCommand = COMMAND_READ_PAGE9;
        QUERY_COMMAND[COMMAND_INDEX] = COMMAND_READ_PAGE9;
        SerialController.getInstance().sendData(QUERY_COMMAND);
    }

    public void setPage9(int year, int month, int day, int hour) {
        mCurrentCommand = COMMAND_WRITE_PAGE9;
        WRITE_COMMAND[COMMAND_INDEX] = COMMAND_WRITE_PAGE9;
        WRITE_COMMAND[4] = (byte) ((year / 100) % 100);
        WRITE_COMMAND[5] = (byte) (year % 100);
        WRITE_COMMAND[6] = (byte) month;
        WRITE_COMMAND[7] = (byte) day;
        WRITE_COMMAND[8] = (byte) hour;
        SerialController.getInstance().sendData(WRITE_COMMAND);
    }

    public void getPage11() {
        mCurrentCommand = COMMAND_READ_PAG11;
        QUERY_COMMAND[COMMAND_INDEX] = COMMAND_READ_PAG11;
        SerialController.getInstance().sendData(QUERY_COMMAND);
    }

    public void setPage11(int minute, int type, int companyId, int employeeId) {
        mCurrentCommand = COMMAND_WRITE_PAG11;
        WRITE_COMMAND[COMMAND_INDEX] = COMMAND_WRITE_PAG11;
        WRITE_COMMAND[4] = (byte) (minute);
        WRITE_COMMAND[5] = (byte) (type);
        WRITE_COMMAND[6] = (byte) companyId;
        WRITE_COMMAND[7] = (byte) employeeId;
        SerialController.getInstance().sendData(WRITE_COMMAND);
    }


    /**
     * 获取温湿度信息
     */
    public void getHumiture() {
        mCurrentCommand = COMMAND_HUMITURE_READ;
        SerialController.getInstance().sendData(HUMITURE_COMMAND);
    }

    public int increaseSendPackageCount() {
        this.mSendPackageCount++;
        return this.mSendPackageCount;
    }

    /**
     * 获取收到的包裹数
     *
     * @return 已收到多少个包裹
     */
    public int getReceivePackageCount() {
        return this.mReceivePackageCount;
    }


    /**
     * 重置发送包裹和接收包裹计数器
     */
    public void resetReceiveAndSendPackageCounter() {
        this.mSendPackageCount = 0;
        this.mReceivePackageCount = 0;
    }

    /**
     * 将包推动给
     */
    public void sendPackage() {
        Log.e("PipProtocol","current command:"+this.getCurrentCommand()+"-"+this.version);
        switch (this.getCurrentCommand()) {

            case COMMAND_READ_PAGE9:
                this.getPage9();
                break;
            case COMMAND_READ_PAG11:
                this.getPage11();
                break;
            case COMMAND_BATTERY_LEVEL:
                this.getBaterryLevel();
                break;
            case COMMAND_HUMITURE_READ:
                this.getHumiture();
                break;
            // case COMMAND_READ_PAGE3:
            case COMMAND_READ_PAGE3:
                this.getPipeId();
                break;

        }
    }

    public void toNext() {
        switch (this.getCurrentCommand()) {
            case COMMAND_READ_PAGE3:
                mCurrentCommand = COMMAND_READ_PAGE9;
                break;
            case COMMAND_READ_PAGE9:
                mCurrentCommand = COMMAND_BATTERY_LEVEL;
                break;
            case COMMAND_BATTERY_LEVEL:
                mCurrentCommand = COMMAND_HUMITURE_READ;
                break;
            case COMMAND_HUMITURE_READ:
                mCurrentCommand = COMMAND_READ_PAG11;
                break;
            case COMMAND_READ_PAG11:
                mCurrentCommand = COMMAND_READ_PAGE3;
        }
        Log.e("下一個：","Current command:" + mCurrentCommand);
        resetReceiveAndSendPackageCounter();
    }

    public Byte getCurrentCommand() {
        return this.mCurrentCommand;
    }

    public void increaseReceivePackage() {
        this.mReceivePackageCount++;
    }
}
